package com.ytjj.qmyx.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.RedisConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.request.complaints.ComplaintsRegistrationRequest;
import com.ytjj.qmyx.admin.service.WxComplaintsService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import com.ytjj.qmyx.mall.model.ComplaintsHistoryResponse;
import com.ytjj.qmyx.mall.client.MallClient;
import com.ytjj.qmyx.mall.model.WxComplaintsListRequest;
import com.ytjj.qmyx.mall.model.WxComplaintsListResponse;
import com.ytjj.qmyx.mall.model.WxComplaintsReplyRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 微信投诉相关接口
 */
@RestController
@RequestMapping("/wxComplaints")
@Slf4j
@RefreshScope
public class WxComplaintsController extends BaseController {

    @Resource
    private MallClient mallClient;

    @Resource
    private WxComplaintsService wxComplaintsService;

    @Resource
    private RedisService redisService;


    /**
     * 查询投诉单协商历史
     * @param complaintId
     * @return
     * @throws Exception
     */
    @GetMapping("/wxComplaintsV3History")
    public CommonResult<List<ComplaintsHistoryResponse>> wxComplaintsV3History(@RequestParam String complaintId) throws Exception{
        return mallClient.wxComplaintsV3History(complaintId);
    }


    /**
     * 回复用户
     * @param replyRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/wxComplaintsV3Response")
    CommonResult wxComplaintsV3Response(@RequestBody WxComplaintsReplyRequest replyRequest) throws Exception{
        Integer adminId = UserUtil.getAdminId();
        replyRequest.setAdminId(adminId);
        //保存输截取后的字符串到缓存，方便下次用
        if (null != replyRequest.getTemplateValue() && !CollectionUtils.isEmpty(replyRequest.getPlaceHolders())) {
            redisService.set(RedisConstants.MSG_TEMPLATE_VARIABLE_KEY + ":" + replyRequest.getTemplateValue() + ":" + adminId, JSON.toJSONString(replyRequest.getPlaceHolders()));
        }
        return mallClient.wxComplaintsV3Response(replyRequest);
    }

    /**
     * 反馈处理完成
     * @param complaintId
     * @return
     * @throws Exception
     */
    @GetMapping("/wxComplaintsV3Complete")
    CommonResult wxComplaintsV3Complete(@RequestParam("complaintId") String complaintId)throws Exception{
        Integer adminId = UserUtil.getAdminId();
        return mallClient.wxComplaintsV3Complete(complaintId,adminId);
    }

    /**
     * 商户上传反馈图片
     * @param file
     * @param complaintId
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public CommonResult getImgUrl(@RequestParam("file") MultipartFile file,@RequestParam("complaintId") String complaintId) throws Exception{
        return CommonResult.success(wxComplaintsService.getImgUrl(file,complaintId));
    }

    /**
     * 查询投诉单列表
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/wxComplaintsV3List")
    public CommonResult<CommonPage<WxComplaintsListResponse>> wxComplaintsV3List(@RequestBody WxComplaintsListRequest request) throws Exception {
        return CommonResult.success(wxComplaintsService.wxComplaintsV3List(request));
    }

    /**
     * 保存客服需登记信息
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/saveComplaintsRegistration")
    public CommonResult saveComplaintsRegistration(@RequestBody ComplaintsRegistrationRequest request) throws Exception {
        Admin admin = UserUtil.getAdmin();
        return CommonResult.success(wxComplaintsService.saveComplaintsRegistration(request,admin));
    }

    /**
     * 查看投诉客服登记信息
     * @param complaintId
     * @return
     */
    @GetMapping("/queryComplaintsRegistration")
    public CommonResult queryComplaintsRegistration(@RequestParam("complaintId") String complaintId) {
        return CommonResult.success(wxComplaintsService.queryComplaintsRegistration(complaintId));
    }
}
